<template>
	<view class="home">
		<!-- 1. 搜索框 -->
		<view class="myHeader">
			<image src="/static/images/logo.png" class="logo" mode=""></image>
			<view class="search">
				<i class='iconfont icon-sousuo'></i><input type="text" placeholder-style='font-size:12px'
					placeholder="请输入...">
			</view>
			<view class="btn">
				搜索
			</view>
		</view>
		<!-- 2. 滑动导航区域 -->
		<scroll-view scroll-x enable-flex class="myNav">
			<view class="item" :class="{active:activeIndex == -1}" @click="changeIndex(-1)">推荐</view>
			<view class="item" :class="{active:index == activeIndex}" v-for="(item,index) in navList" :key="item.L1Id"
				@click="changeIndex(index,item.L1Id)">{{item.text}}</view>

		</scroll-view>
		
		<!-- 3. 推荐界面 -->
		<Recommend v-if="activeIndex == -1" :policyDescList='policyDescList' :goodsList='goodsList'></Recommend>	
		<!-- 4. 其他导航类目--- 居家生活 服饰鞋包 等-->
		<MenuCate v-else :cateId='cateId' :scrollTop='scrollTop'></MenuCate>
		
	</view>
</template>

<script>
	import base from '@/utils/base.js'
	import request from '@/utils/request.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				navList: [], //滑动导航区域容器
				activeIndex: -1, //高亮的变量标识
				policyDescList:[],//icon信息 品牌信息
				goodsList:[],//好物推荐
				cateId:'',
				scrollTop:0,
			}
		},
		computed: {
			// ...mapState('menu',['navList'])
		},
		//监听页面滚动事件
		onPageScroll(e) { 
			//参数：eventName	String	事件名    OBJECT 	Object	触发事件携带的附加参数
			uni.$emit('paseScroll',e.scrollTop)
		},
		created() {
			this.getIndexData()
		},
		methods: {
			...mapMutations('menu', ['setNavList']),
			//请求首页的动态数据信息-------------------------
			async getIndexData() {
				let res = await request(base.indexData)
				console.log('首页动态数据---', res);
				//导航区域
				this.navList = res.indexData.kingKongModule.kingKongList;
				//品牌信息
				this.policyDescList = res.indexData.policyDescList
				//推荐界面-好物推荐
				this.goodsList = res.indexData.categoryModule
				//导航区域数据-存储仓库 vuex
				this.setNavList(res.indexData.kingKongModule.kingKongList)
			},
			//点击导航高亮-----------------
			changeIndex(index,id) {
				this.activeIndex = index
				//把点击的导航分类的id传递给子组件导航分类组件 --对应的获取当前的导航类目的动态数据
				this.cateId = id;
				// console.log('id-----',id,this.cateId);
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 搜索
	.myHeader {
		display: flex;
		padding: 10rpx;
		align-items: center;
		font-size: 24rpx;

		.logo {
			width: 140rpx;
			height: 40rpx;
		}

		.search {
			border: 1px solid #eee;
			border-radius: 10rpx;
			flex: 1;
			margin: 0 10rpx;
			// padding-left: 14rpx;
			display: flex;
			align-items: center;
			padding: 4rpx 10rpx;
			vertical-align: top;
		}

		.btn {
			width: 80rpx;
			text-align: center;
		}
	}

	//横向滚动
	.myNav {
		white-space: nowrap;
		display: flex;
		color: #333;
		height: 70rpx;

		.item {
			// flex: 1;
			display: inline-block;
			margin: 10rpx 16rpx;
			text-align: center;

			&.active {
				color: #BB2C08;
				border-bottom: 1px solid #BB2C08;
			}
		}

	}
</style>
